/** @jsxImportSource @emotion/react */
import {
  CCell, Cell, InputLine, LineColumn, SuffixInput, Table, TableBody, TableHead, TableRow, Text
} from "customize-easy-ui-component";
import * as React from "react";
import {InspectRecordLayout, InternalItemProps, RepLink, SelectInput, useItemInputControl} from "../../common/base";
import {calcAverageArrObj} from "../../../common/tool";
import {useMeasureInpFilter} from "../../common/hooks";
import {检查结论选} from "../../tools";

export const tail自重 = <Text css={{"@media print": {fontSize: '0.875rem'}}}>
  注：额定起重量≤10000kg，允差为设计值±3%。<br/>
  额定起重量＞10000kg，允差为设计值±5%或者设计值±1000，取少者。
</Text>;
export const itemA自重 = ['自重正', '自重反', '自重测果', '自重r'];
const itemA自重m = itemA自重.concat('整机质量');
/**附表2
 * */
export const SelfWeight =
    React.forwardRef((
        {children, show, alone = true, redId, nestMd, label,}: InternalItemProps, ref
    ) => {
      const [getInpFilter] = useMeasureInpFilter(null, itemA自重m,);
      const {inp, setInp} = useItemInputControl({ref});
      const aveWeig = calcAverageArrObj([inp?.自重正, inp?.自重反], (row) => row, 0);
      return <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show} redId={redId}
                                  nestMd={nestMd} alone={alone} label={label!}>
        <Text variant="h5">{label}：</Text>
        自重设计值 ={inp?.整机质量 ?? '—'}（kg）
        <LineColumn column={4}>
          <InputLine label='正向'>
            <SuffixInput value={inp?.自重正 || ''}
                         onChange={e => setInp({...inp, 自重正: e.currentTarget.value || undefined})}>kg</SuffixInput>
          </InputLine>
          <InputLine label='反向'>
            <SuffixInput value={inp?.自重反 || ''}
                         onChange={e => setInp({...inp, 自重反: e.currentTarget.value || undefined})}>kg</SuffixInput>
          </InputLine>
          <InputLine label='测定结果'>
            <SuffixInput value={inp?.自重测果 || ''}
                         onChange={e => setInp({...inp, 自重测果: e.currentTarget.value || undefined})}>kg</SuffixInput>
          </InputLine>
        </LineColumn>
        自重平均值={aveWeig}（kg）
        <InputLine label='结果判定'>
          <SelectInput value={inp?.自重r || ''} list={检查结论选}
                       onChange={e => setInp({...inp, 自重r: e.currentTarget.value || undefined})}/>
        </InputLine>
        {tail自重}
      </InspectRecordLayout>;
  });

/**附表2
 * */
export const SelfWeightVw = ({orc, rep, label,}: { orc: any, rep: any, label: string }
) => {
  const aveWeig = calcAverageArrObj([orc?.自重正, orc?.自重反], (row) => row, 0);
  return <>
    <Text id='SelfWeight' variant="h4" css={{
      marginTop: '1rem',
    }}>{label}</Text>
    <Table fixed={["18%", "18%", "18%", "17%", "%", "12%"]} css={{borderCollapse: 'collapse'}} tight miniw={800}>
      <TableHead>
        <TableRow>
          <CCell>正向（kg）</CCell><CCell>反向（kg）</CCell><CCell>平均值（kg）</CCell>
          <CCell>设计值（kg）</CCell><CCell>测定结果（kg）</CCell><CCell>结果判定</CCell>
        </TableRow>
      </TableHead>
      <TableBody>
        <RepLink ori rep={rep} tag={'SelfWeight'}>
          <TableRow>
            <CCell>{orc?.自重正}</CCell><CCell>{orc?.自重反}</CCell><CCell>{aveWeig}</CCell>
            <CCell>{orc?.整机质量 ?? '—'}</CCell><CCell>{orc?.自重测果}</CCell><CCell>{orc?.自重r ?? '—'}</CCell>
          </TableRow>
          <TableRow>
            <Cell colSpan={6} css={{whiteSpace: 'pre-wrap'}}>{tail自重}</Cell>
          </TableRow>
        </RepLink>
      </TableBody>
    </Table>
  </>;
};
